/*
 * author      : dormi330
 * date        : 2018/7/17
 * description : TODO
 */

package com.deep.trade.application.refund;

import com.deep.trade.adaptor.required.catalog.Catalog;
import com.deep.trade.adaptor.required.catalog.CatalogService;
import com.deep.trade.common.TradeException;
import com.deep.trade.domain.order.*;
import com.deep.trade.domain.refund.*;
import com.deep.trade.refund.dto.InDtoAuditRefundApply;
import com.deep.trade.refund.dto.InDtoRefundApply;
import com.deep.trade.refund.dto.InDtoRefundType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RefundService {

    @Autowired
    private CommandAddRefundType commandAddRefundType;

    @Autowired
    private CommandApplyRefund commandApplyRefund;

    @Autowired
    private OrderQueryRepository orderQueryRepository;

    @Autowired
    private CatalogService catalogService;

    @Autowired
    private RefundQueryRepository refundQueryRepository;

    @Autowired
    private OrderRepository orderRepository;

    @Autowired
    private RefundRepository refundRepository;

    // 添加退款类型
    @Transactional
    public void addNewRefundType(InDtoRefundType inDtoRefundType) throws Exception {
        Catalog catalog = catalogService.getCatalogById(inDtoRefundType.getCatalogId());
        commandAddRefundType.addRefundType(catalog, inDtoRefundType.getTypeCode(), inDtoRefundType.getTypeName());
    }

    // 申请退款
    public void applyRefund(InDtoRefundApply apply) throws Exception {
        // 组装 TradeOrder
        TradeOrder tradeOrder = orderQueryRepository.tradeOrderOfId(apply.getTradeOrderId());
        if (null == tradeOrder) throw new TradeException("no such order");
        if (!tradeOrder.canRefund()) throw new TradeException("this order cannot refund");

        List<TradeOrderSku> tradeOrderSkuList = orderQueryRepository.orderSkuOfTradeOrder(apply.getTradeOrderId());
        tradeOrder.setSkuList(tradeOrderSkuList);

        // 获取 refundType
        RefundType refundType = refundQueryRepository.refundTypeOfId(apply.getRefundTypeId());
        if (null == refundType) throw new TradeException("no such refund type");

        // 进行操作
        commandApplyRefund.applyRefund(tradeOrder, apply.getSkuIds(), apply.getRefundMoney(), refundType, apply.getDescription());
    }

    // 卖家审核退款单
    @Transactional
    public void auditRefundApply(InDtoAuditRefundApply inDtoAuditRefundApply) throws Exception {
        RefundOrder refundOrder = refundQueryRepository.refundApplyOfId(inDtoAuditRefundApply.getRefundApplyId());
        if (null == refundOrder) throw new TradeException("no such refund apply");

        // 更新 退款单
        refundOrder.audit(inDtoAuditRefundApply.isAccept(), inDtoAuditRefundApply.getRejectReason());
        refundRepository.updateRefundApplyStatus(refundOrder);

        // 更新 订单
        TradeOrder tradeOrder = orderQueryRepository.tradeOrderOfId(refundOrder.getTradeOrderId());
        tradeOrder.setStatusEnum(OrderStatusEnum.REFUND_APPLIED);
        orderRepository.updateTradeOrderStatus(tradeOrder);
    }

}
